<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()" ref="dataForm">
      <el-form-item label="操作">
        <el-select v-model="dataForm.operateAction" placeholder="请选择">
          <el-option
            v-for="item in useroption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="时间筛选">
        <el-select
        style="width:150px;"
        @change="dateChanged"
        v-model="dataForm.currentDate"
        placeholder="请选择"
      >
        <el-option v-for="item in dateSelect" :key="item" :label="item" :value="item"></el-option>
      </el-select>
        <template v-if="showDatePicker">
          <el-date-picker
            style="width:220px;"
            v-model="dataForm.startTime"
            type="datetime"
            :picker-options="datePickerOptions"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="开始时间"
          ></el-date-picker>~
          <el-date-picker
            style="width:220px;"
            v-model="dataForm.endTime"
            type="datetime"
            :picker-options="datePickerOptions"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="结束时间"
          ></el-date-picker>
        </template>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.transactionId" placeholder="微信交易单号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="search()">查询</el-button>
        <el-button type="primary" @click="goBack">返回</el-button>
      </el-form-item>
    </el-form>
    <div class="integral">
      <div>当前可用积分：{{ totalStatistic.currentPoint }}分</div>
      <div>获得积分：{{ totalStatistic.getPoint }}分</div>
      <div>扣除积分：{{ totalStatistic.usePoint }}分</div>
    </div>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;"
    >
      <el-table-column prop="gmtCreate" header-align="center" align="center" label="时间"></el-table-column>
      <el-table-column header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          {{ scope.row.operateAction === 1?'销售':'' }}
          {{ scope.row.operateAction === 2?'全部退款':'' }}
          {{ scope.row.operateAction === 3?'部分退款':'' }}
          {{ scope.row.operateAction === 4?'后台手动加减':'' }}
          {{ scope.row.operateAction === 5?'资源兑换':'' }}
          {{ scope.row.operateAction === 6?'资源竞拍':'' }}
          {{ scope.row.operateAction === 7?'竞拍失败':'' }}
          {{ scope.row.operateAction === 8?'历史积分':'' }}
          {{ scope.row.operateAction === 9?'竞拍加价':'' }}
        </template>
      </el-table-column>
      <el-table-column prop="operateActionName" header-align="center" align="center" label="操作名称"></el-table-column>
      <el-table-column prop="orderNo" header-align="center" align="center" label="订单号"></el-table-column>
      <el-table-column prop="transactionId" header-align="center" align="center" label="微信交易单号"></el-table-column>
      <el-table-column header-align="center" align="center" label="积分">
        <template slot-scope="scope">
          {{ scope.row.operateType === 1?'+':'-' }}{{ scope.row.pointNum }}
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        showDatePicker: false,
        useroption: [
          {
            value: null,
            label: '全部'
          },
          {
            value: '1',
            label: '销售'
          },
          {
            value: '2',
            label: '全部退款'
          },
          {
            value: '3',
            label: '部分退款'
          },
          {
            value: '4',
            label: '后台手动加减'
          },
          {
            value: '5',
            label: '资源兑换'
          },
          {
            value: '6',
            label: '资源竞拍'
          },
          {
            value: '7',
            label: '竞拍失败'
          },
          {
            value: '8',
            label: '历史积分'
          },
          {
            value: '9',
            label: '竞拍加价'
          }
        ],
        dateSelect: [
          '全部',
          "昨日",
          "本周",
          "上周",
          '本月',
          "最近30天",
          "上月",
          "自定义"
        ],
        datePickerOptions: {
          disabledDate (time) {
            return (
              time.getTime() > Date.now() ||
              time.getTime() < Date.now() - 30 * 24 * 60 * 60 * 1000
            );
          }
        },
        dataForm: {},
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        totalStatistic: {}
      }
    },
    mounted() {
      // let obj = {
      //   preDay: 1,
      //   day: 0
      // };
      // let timeDate = this.getDateData(obj);
      // this.dataForm.startDate = timeDate.startDate;
      // this.dataForm.endDate = timeDate.endDate;
    },
    methods: {
      initIntegral(id) {
        this.dataForm.userId = id
        this.getDataList()
        this.getDataIntegral()
      },
      //时间select change事件
      dateChanged(val) {
        let obj = {};
        if (val === "全部"){
          delete this.dataForm.startDate
          delete this.dataForm.endDate
          return false
        }
        if (val === "昨日") {
          obj.preDay = 1;
          obj.day = 0;
        } else if (val === "本周") {
          let num = new Date().getDay();
          if (num === 0) {
            obj.preDay = 6;
            obj.day = 6;
          } else {
            obj.preDay = num-1;
            obj.day = 6;
          }
        } else if (val === "上周") {
          let num = new Date().getDay() === 0 ? 7 : new Date().getDay();
          obj.preDay = num + 6;
          obj.day = 6;
        } else if (val === "本月") {
          let num = new Date().getDate();
          obj.preDay = num - 1;
          obj.day = num - 1;
        } else if (val === "最近30天") {
          obj.preDay = 29;
          obj.day = 29;
        } else if (val === "上月") {
          let date = new Date();
          let month = date.getMonth() - 1;
          date.setMonth(month + 1);
          date.setDate(0);
          let monthNum = date.getDate();
          let num = monthNum + new Date().getDate();
          obj.preDay = num-1;
          obj.day = monthNum-1;
        }

        if (val === "自定义") {
          this.dataForm.startDate = "";
          this.dataForm.endDate = "";
          this.showDatePicker = true;
          return;
        } else {
          let timeDate = this.getDateData(obj);
          console.log("timeDate====>", timeDate);
          this.dataForm.startDate = timeDate.startDate;
          console.log("this.dataForm.endDate====>", timeDate.endDate);
          this.dataForm.endDate = timeDate.endDate;
          // if (val === "本周" || val === "最近30天") {
          //   this.dataForm.endDate = new Date();
          // } else {
          //   this.dataForm.endDate = timeDate.endDate;
          // }
        }
        this.showDatePicker = false;
      },
      getDateData(data) {
        let obj = {};
        obj.startDate = "";
        obj.endDate = "";
        if (data && data.preDay + "") {
          let time = Date.now();
          let startDate = getDateText(time - data.preDay * 24 * 3600 * 1000);
          let endDate = getDateText(
            time - data.preDay * 24 * 3600 * 1000 + data.day * 24 * 3600 * 1000
          );
          obj.startDate = startDate;
          obj.endDate = endDate;
        }

        function getDateText(timeMinutes) {
          let date = new Date(timeMinutes);
          let year = date.getFullYear();
          let month =
            date.getMonth() > 8
              ? date.getMonth() + 1
              : "0" + (date.getMonth() + 1);
          let dayText =
            date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
          console.log(dayText)
          return year + "-" + month + "-" + dayText;
        }
        return obj;
      },
      search() {
        this.pageIndex = 1
        this.getDataList()
        this.getDataIntegral()
      },
      // 获取积分分数
      getDataIntegral() {
        // this.dataListLoading = true
        this.dataForm.page = this.pageIndex
        this.dataForm.limit = this.pageSize
        this.$http({
          url: this.$http.adornUrl('/point/userrewardpointlog/getStatisticByCondition'),
          method: 'get',
          params: this.$http.adornParams(this.dataForm)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.totalStatistic = data.totalStatistic
          }
        })
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        this.dataForm.page = this.pageIndex
        this.dataForm.limit = this.pageSize
        this.$http({
          url: this.$http.adornUrl('/point/userrewardpointlog/list'),
          method: 'get',
          params: this.$http.adornParams(this.dataForm)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$refs['dataForm'].resetFields()
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      goBack() {
        this.$emit('goBack')
      }
    }
  }
</script>
<style scoped>
  .integral{
    display: flex;
  }
  .integral>div{
    margin: 15px;
  }
</style>
